<template>
  <div class="add-edit">
    <el-dialog :title="title" :visible.sync="centerDialogVisible" width="45%" @close="closeDialog">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="110px" class="demo-ruleForm">
        <el-form-item label="人员名称" prop="userName">
          <el-input v-model="formData.userName" placeholder="请输入" maxlength="5" show-word-limit />
        </el-form-item>
        <!-- 角色列表 -->
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="formData.roleId" placeholder="请输入">
            <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入" maxlength="11" show-word-limit />
        </el-form-item>
        <el-form-item label="负责区域" prop="regionName">
          <el-select v-model="formData.regionName" placeholder="请输入">
            <el-option v-for="item in regionList" :key="item.id" :label="item.name" :value="item.name" />

          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="image">
          <el-upload class="avatar-uploader" action="" :show-file-list="false" :http-request="handleAvatarSuccess">
            <img v-if="formData.image" :src="formData.image" class="avatar">
            <i v-else class="el-icon-upload avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="状态">
          <el-checkbox v-model="formData.status">是否启用</el-checkbox>
        </el-form-item>
        <div class="btns" style="text-align: center">
          <el-button class="cancel" style="margin-right: 18px;" @click="closeDialog">取消</el-button>
          <el-button type="warning" @click="submitForm('ruleForm')">确认</el-button>
        </div>

      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleListRequest, getPeopleDetailRequest, getRegionListRequest, editPeopleInfoRequest, addPeopleRequest } from '../../../api/people.js'
export default {
  name: '',
  props: {
    title: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      centerDialogVisible: false,
      formData: {
        userName: '',
        roleId: null,
        mobile: '',
        regionld: '',
        status: true,
        image: '',
        regionName: ''
      },
      // 角色列表
      roleList: [],

      rules: {
        userName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        regionld: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]

      },
      regionList: [],
      currentId: ''

    }
  },
  computed: {
  },
  created() {
    this.getRoleList()
    this.getRegionList()
  },
  mounted() {

  },
  methods: {
    // 获取区域列表
    async getRegionList() {
      const res = await getRegionListRequest()
      console.log(res, 6766)
      this.regionList = res.data.currentPageRecords
    },
    // 点击提交 修改/添加
    async submitForm() {
      if (this.title === '编辑人员') {
        // 编辑
        await editPeopleInfoRequest(this.formData, this.currentId)
        this.closeDialog()
        // 刷新父组件列表
        this.$emit('success')
        return
      }

      // 添加
      console.log('wwwww')
      console.log(this.formData)
      const res = await addPeopleRequest(this.formData)
      console.log(res, 6767)
      console.log(this.formData, 'add')
      this.closeDialog()
    },
    // 清空表单
    resetForm() {
      this.formData = this.$options.data().formData
    },
    // 数据回显
    async getPeopleDetail(id) {
      this.currentId = id
      const res = await getPeopleDetailRequest(id)
      console.log(res, '编辑')
      this.formData = res.data
    },
    // 获取角色列表
    async getRoleList() {
      console.log(77)
      const res = await getRoleListRequest()
      this.roleList = res.data
      console.log(this.roleList)
    },
    // 打开对话框
    openDialog() {
      this.centerDialogVisible = true
    },

    // 关闭对话框
    closeDialog() {
      this.centerDialogVisible = false
      this.resetForm()
    },

    handleAvatarSuccess() {

    }
  }
}
</script>
<style lang='scss'  scoped>
.btns {
  .cancel {
    background-color: #f2dcce;
    border: 0;
  }
}

::v-deep .el-dialog {
  border-radius: 12px;
}

::v-deep .el-dialog__title {
  font-weight: bold;
}

::v-deep .el-dialog__body {
  padding: 20px 20px 30px;
  font-weight: 0;

  .el-form {
    padding: 0 20px;
    font-weight: 0;
  }

  .el-select {
    width: 100%;
  }

  .el-input {
    width: 110%;
  }

  .btns {
    margin: 0 auto;
  }
}
</style>

<style>
.avatar-uploader img{
   width: 78px;
   height: 78px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
